<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Spring 事件机制深度解析</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .content-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            transition: all 0.3s ease;
        }
        
        .content-card:hover {
            transform: translateY(-2px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
        }
        
        .code-block {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 12px;
            padding: 1.5rem;
            overflow-x: auto;
            font-family: 'Consolas', 'Monaco', monospace;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
        }
        
        .section-title {
            position: relative;
            padding-left: 1rem;
        }
        
        .section-title::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 80%;
            background: linear-gradient(180deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        
        .feature-card {
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
            color: white;
            transition: all 0.3s ease;
        }
        
        .feature-card:hover {
            transform: scale(1.05);
            box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
        }
        
        .mermaid {
            background: white;
            padding: 2rem;
            border-radius: 12px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
        }
        
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 1;
            font-weight: 700;
            margin-right: 0.5rem;
            color: #667eea;
            font-family: 'Noto Serif SC', serif;
        }
        
        .highlight-text {
            background: linear-gradient(120deg, #f093fb 0%, #f5576c 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
            font-weight: 600;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20 px-6">
        <div class="max-w-6xl mx-auto text-center">
            <h1 class="text-5xl md:text-6xl font-bold mb-6 animate-pulse">
                <i class="fas fa-broadcast-tower mr-4"></i>
                Spring 事件机制深度解析
            </h1>
            <p class="text-xl md:text-2xl opacity-90 max-w-3xl mx-auto leading-relaxed">
                探索 Spring 框架中优雅的发布-订阅模式，掌握组件间解耦通信的艺术
            </p>
            <div class="mt-10 flex justify-center space-x-8">
                <div class="text-center">
                    <i class="fas fa-layer-group text-4xl mb-2"></i>
                    <p class="text-lg">松耦合架构</p>
                </div>
                <div class="text-center">
                    <i class="fas fa-sync-alt text-4xl mb-2"></i>
                    <p class="text-lg">异步处理</p>
                </div>
                <div class="text-center">
                    <i class="fas fa-puzzle-piece text-4xl mb-2"></i>
                    <p class="text-lg">灵活扩展</p>
                </div>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="max-w-6xl mx-auto px-6 py-12">
        
        <!-- Section 1: 概述 -->
        <div class="content-card rounded-2xl shadow-xl p-8 mb-8">
            <h2 class="section-title text-3xl font-bold mb-6 text-gray-800">
                <i class="fas fa-info-circle mr-3 text-purple-600"></i>
                Spring 事件机制概述
            </h2>
            <p class="text-lg leading-relaxed text-gray-700 mb-4">
                <span class="drop-cap">S</span>pring 事件机制是 Spring 框架的一个重要特性，它基于发布-订阅模式实现。通过事件机制，应用的不同部分可以相互通信而不直接依赖于彼此。这种机制通常用来解耦各个组件之间的关系，使得各个模块的功能更加独立。
            </p>
            <p class="text-lg leading-relaxed text-gray-700">
                Spring 事件机制由两部分组成：<span class="highlight-text">事件发布者</span>（Publisher）和<span class="highlight-text">事件监听器</span>（Listener）。发布者发布事件，监听器监听这些事件并做出响应。Spring 事件机制默认基于 <code class="bg-gray-200 px-2 py-1 rounded text-purple-600">ApplicationEvent</code> 类和 <code class="bg-gray-200 px-2 py-1 rounded text-purple-600">ApplicationListener</code> 接口来实现。
            </p>
        </div>

        <!-- 核心概念可视化 -->
        <div class="content-card rounded-2xl shadow-xl p-8 mb-8">
            <h3 class="text-2xl font-bold mb-6 text-gray-800 text-center">
                <i class="fas fa-sitemap mr-3 text-purple-600"></i>
                Spring 事件机制架构图
            </h3>
            <div class="mermaid">
                graph TB
                    A[事件发布者<br/>Event Publisher] -->|发布事件| B[ApplicationContext]
                    B -->|委托| C[事件多播器<br/>EventMulticaster]
                    C -->|分发| D[监听器1<br/>Listener 1]
                    C -->|分发| E[监听器2<br/>Listener 2]
                    C -->|分发| F[监听器3<br/>Listener 3]
                    
                    style A fill:#f093fb,stroke:#333,stroke-width:2px,color:#fff
                    style B fill:#667eea,stroke:#333,stroke-width:2px,color:#fff
                    style C fill:#764ba2,stroke:#333,stroke-width:2px,color:#fff
                    style D fill:#4facfe,stroke:#333,stroke-width:2px,color:#fff
                    style E fill:#4facfe,stroke:#333,stroke-width:2px,color:#fff
                    style F fill:#4facfe,stroke:#333,stroke-width:2px,color:#fff
            </div>
        </div>

        <!-- Section 2: 核心类 -->
        <div class="content-card rounded-2xl shadow-xl p-8 mb-8">
            <h2 class="section-title text-3xl font-bold mb-6 text-gray-800">
                <i class="fas fa-cube mr-3 text-purple-600"></i>
                Spring 事件机制的核心类
            </h2>
            <div class="grid grid-cols-1 md:grid-cols-2 gap-6 mb-8">
                <div class="feature-card p-6 rounded-xl">
                    <i class="fas fa-calendar-check text-3xl mb-3"></i>
                    <h4 class="text-xl font-bold mb-2">ApplicationEvent</h4>
                    <p>所有事件的基类，承载事件数据</p>
                </div>
                <div class="feature-card p-6 rounded-xl">
                    <i class="fas fa-headphones text-3xl mb-3"></i>
                    <h4 class="text-xl font-bold mb-2">ApplicationListener</h4>
                    <p>事件监听器接口，用于监听和响应事件</p>
                </div>
                <div class="feature-card p-6 rounded-xl">
                    <i class="fas fa-bullhorn text-3xl mb-3"></i>
                    <h4 class="text-xl font-bold mb-2">ApplicationEventPublisher</h4>
                    <p>事件发布者接口，用于发布事件</p>
                </div>
                <div class="feature-card p-6 rounded-xl">
                    <i class="fas fa-satellite-dish text-3xl mb-3"></i>
                    <h4 class="text-xl font-bold mb-2">SimpleApplicationEventMulticaster</h4>
                    <p>事件多播器，负责事件分发</p>
                </div>
            </div>

            <h3 class="text-2xl font-bold mb-4 text-gray-800">
                <i class="fas fa-code mr-2 text-purple-600"></i>
                ApplicationEvent 类示例
            </h3>
            <pre class="code-block"><code>public class MyEvent extends ApplicationEvent {
    private String message;

    public MyEvent(Object source, String message) {
        super(source);
        this.message = message;
    }

    public String getMessage() {
        return message;
    }
}</code></pre>
            <p class="text-gray-700 mt-4 text-lg">
                这是一个自定义事件，继承了 <code class="bg-gray-200 px-2 py-1 rounded text-purple-600">ApplicationEvent</code>，用于携带要传递的数据（如 <code class="bg-gray-200 px-2 py-1 rounded text-purple-600">message</code>）。
            </p>
        </div>

        <!-- Section 3: 事件发布流程 -->
        <div class="content-card rounded-2xl shadow-xl p-8 mb-8">
            <h2 class="section-title text-3xl font-bold mb-6 text-gray-800">
                <i class="fas fa-paper-plane mr-3 text-purple-600"></i>
                事件发布的实现流程
            </h2>
            <p class="text-lg leading-relaxed text-gray-700 mb-6">
                Spring 提供了一个简单的事件发布机制，通常通过 <code class="bg-gray-200 px-2 py-1 rounded text-purple-600">ApplicationContext</code> 来发布事件。<code class="bg-gray-200 px-2 py-1 rounded text-purple-600">ApplicationContext</code> 实现了 <code class="bg-gray-200 px-2 py-1 rounded text-purple-600">ApplicationEventPublisher</code> 接口，因此它可以发布事件。
            </p>

            <div class="bg-purple-50 border-l-4 border-purple-600 p-6 rounded-r-lg mb-6">
                <h4 class="text-xl font-bold mb-3 text-purple-800">
                    <i class="fas fa-list-ol mr-2"></i>
                    事件发布步骤
                </h4>
                <ol class="space-y-2 text-gray-700">
                    <li><i class="fas fa-check-circle text-purple-600 mr-2"></i>获取 ApplicationContext 实例</li>
                    <li><i class="fas fa-check-circle text-purple-600 mr-2"></i>使用 publishEvent() 方法发布事件</li>
                </ol>
            </div>

            <h3 class="text-2xl font-bold mb-4 text-gray-800">
                <i class="fas fa-code mr-2 text-purple-600"></i>
                事件发布示例代码
            </h3>
            <pre class="code-block"><code>public class MyEventPublisher {
    @Autowired
    private ApplicationContext context;

    public void publishEvent(String message) {
        MyEvent event = new MyEvent(this, message);
        context.publishEvent(event);  // 发布事件
    }
}</code></pre>
        </div>

        <!-- Section 4: 事件监听器 -->
        <div class="content-card rounded-2xl shadow-xl p-8 mb-8">
            <h2 class="section-title text-3xl font-bold mb-6 text-gray-800">
                <i class="fas fa-satellite mr-3 text-purple-600"></i>
                事件监听器的实现流程
            </h2>
            <p class="text-lg leading-relaxed text-gray-700 mb-6">
                Spring 中的事件监听器通过实现 <code class="bg-gray-200 px-2 py-1 rounded text-purple-600">ApplicationListener</code> 接口来接收事件。当事件发布时，所有实现了该接口的监听器会被触发。
            </p>

            <h3 class="text-2xl font-bold mb-4 text-gray-800">
                <i class="fas fa-code mr-2 text-purple-600"></i>
                创建事件监听器
            </h3>
            <pre class="code-block"><code>@Component
public class MyEventListener implements ApplicationListener&lt;MyEvent&gt; {
    @Override
    public void on